package com.hertz.appr.service.cmd.v1;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.hertz.appr.pojo.CmdPojo;
import com.hertz.appr.util.CmdHelper;
import com.hertz.appr.util.Util;
import com.hertz.appr.util.WeiboException;

public class UpdateCmdLineServiceImpl implements ICmdLineService{

	private Logger log = Logger.getLogger(UpdateCmdLineServiceImpl.class);
	@Override
	public CommandLine buildCmdLine(String cmdLineStr)throws Exception {
		log.info("build CommandLine 4 " + cmdUpdate + " : " + cmdLineStr);
		CommandLine cl = null;
		if(StringUtils.isNotBlank(cmdLineStr) && !cmdLineStr.equals(cmdUpdate)){//<guess>
			String optionLine = cmdLineStr.substring(cmdLineStr.indexOf("-"),cmdLineStr.length());
			String[] args = optionLine.split(CmdHelper.cmd_split);
			CommandLineParser parser = new GnuParser();
			try {
				Options options = getOptions();
				cl = parser.parse(options, args,false);
			} catch (Exception e) {
				// TODO: handle exception
				log.error("parse guess option error : " + e.getMessage());
				throw new WeiboException(e.getMessage());
			}
		}
		return cl;
	}

	@Override
	public boolean isCmdLine(String cmdLineStr) {
		// TODO Auto-generated method stub
		if(StringUtils.isBlank(cmdLineStr)){
			log.info("cmdLineStr is blank,return false");
			return false;
		}
		if(cmdLineStr.replace("<", "").replace(">", "").startsWith(cmdUpdate)){
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public String checkCommandLine(CommandLine cl){
//		try {
//			Option[] os = cl.getOptions();
//			for(Option o : os){
//				if(o.getOpt().equals(oCount)){
//					String value = o.getValue();
//					if(!StringUtils.isNumeric(value.trim())){
//						log.error("count='" + value + "' format error");
//						return "-" + oCount + "参数必须是正整数，例如5";
//					}
//				}else if(o.getOpt().equals(oInterval)){
//					String value = o.getValue();
//					if(!StringUtils.isNumeric(value.trim())){
//						log.error("interval='" + value + "' format error");
//						return "-" + oInterval + "参数必须是正整数，例如5";
//					}
//				}else if(o.getOpt().equals(oEndtime)){
//					String value = o.getValue();
//					if(!Util.isEndTime((value.trim()))){
//						log.error("endtime='" + value + "' format error");
//						return "-" + oEndtime + "参数值格式错误，形如20110501-01:01:01 或 20110501";
//					}
//				}
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//			log.error("parse action option error : " + e.getMessage());
//			return "error";
//		}
		return "ok";
	}
	
	@Override
	public Short getCmdType(){
		return CmdPojo.cmdType_action;
	}
	
	@Override
	public String toString(){
		return "update cmdlineService";
	}
	
	private Options getOptions(){
		Options options = new Options();
		Option endOption = new Option(oEnd, oEndLong, false, "");
		endOption.setType(Number.class);
		Option answerOption = new Option(oAnswer, oAnswerLong, true, "");
		answerOption.setArgs(1);
//		answerOption.s

		OptionGroup og = new OptionGroup();
		og.addOption(answerOption);
		og.addOption(endOption);
		
		
		options.addOptionGroup(og);
		options.addOption(answerOption);
		return options;
	}
	
}
